package com.plm.maque.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plm.maque.dao.UserMapper;
import com.plm.maque.entity.User;
import com.plm.maque.service.UserService;
import com.plm.maque.utils.PageUtils;
import com.plm.maque.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author Limi Pan
 * @since 2018-12-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        long current = params.get("current") == null ? 1 : Long.valueOf((String) params.get("current"));
        long size = params.get("size") == null ? 10 : Long.valueOf((String) params.get("size"));

        Page<User> pages = (Page<User>) this.page(
                new Page<>(current, size),
                new QueryWrapper<>()
        );

        return new PageUtils(pages);
    }

    @Override
    public User getUser(Map<String, Object> params) {
        String username = (String) params.get("username");
        Integer userid = (Integer) params.get("userid");

        User user = this.getOne(new QueryWrapper<User>()
                .eq(StringUtils.isNotNull(username), "username", username)
                .eq(StringUtils.isNotNull(userid), "user_id", userid)
        );
        return user;
    }
}
